package com.online.studyonline.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.online.studyonline.pojo.ResultT;
import com.online.studyonline.pojo.Studyrecords;
import com.online.studyonline.service.IStudyrecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-28
 */
@RestController
@RequestMapping("/studyrecords")
public class StudyrecordsController {
    @Autowired
    private IStudyrecordsService studyrecordsService;

    @PostMapping("/addDate")
    public ResultT addDate(Integer userId, Integer studyTime){
        int t = studyTime.intValue();
        Studyrecords entity = new Studyrecords();
        entity.setUserId(userId);
        entity.setStudyTime(LocalTime.of(t/60/60,(t/60)%60,t%60));
        entity.setStudyDate(LocalDate.now());
        studyrecordsService.save(entity);
        return ResultT.success();
    }

    @GetMapping("/getAll")
    public ResultT<List<Studyrecords>> getAll(){

        List<Studyrecords> list = studyrecordsService.list();
        return ResultT.success(list);
    }

    @GetMapping("/getById")
    public ResultT<List<Studyrecords>> getById(Integer userId){
        QueryWrapper<Studyrecords> wrapper = new QueryWrapper<Studyrecords>()
                .eq("user_id",userId);

        List<Studyrecords> list  = studyrecordsService.list(wrapper);
        return ResultT.success(list);
    }

}
